package com.ymj;

import com.ymj.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisDemo {
    public static void main(String[] args) throws IOException {
//        1.加载 mybatis 的核心配置文件，获取 SqlsessionFactry
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

//        2.获取 sqlSession 对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        3.执行sql(存在硬编码的问题，后续使用代理对象的形式: sqlSession.getMapper(UserMapper.class) 的方式)
//        见 MybatisDemo2
        List<User> users = sqlSession.selectList("com.ymj.mapper.UserMapper.selectAll");
        System.out.println(users);

//        4.释放资源
        sqlSession.close();

    }
}
